home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / pctv4n2.zip / WHICH.PAS < prev   
Pascal/Delphi Source File  |  1993-06-09  |  3KB  |  99 lines

  1. (* ----------------------------------------------------------- *(
  2. **  which.pas -- Finds executable pathname                     **
  3. ** ----------------------------------------------------------- **
  4. **                                                             **
  5. **  Use WHICH to find out which of several possible            **
  6. **  programs will execute if you enter their names at a        **
  7. **  DOS prompt. For example, if you have a few copies of       **
  8. **  grep.com on your hard drive, enter WHICH GREP to find      **
  9. **  out which one will run if you type GREP <argument>.        **
  10. **                                                             **
  11. **  The program looks for programs in the current directory    **
  12. **  and in all directories on the current PATH. It             **
  13. **  recognizes DOS's default execution order of .COM,          **
  14. **  .EXE, and .BAT files, in that order.                       **
  15. **                                                             **
  16. ** ----------------------------------------------------------- **
  17. **     Copyright (c) 1993 by Tom Swan. All rights reserved.    **
  18. )* ----------------------------------------------------------- *)
  19.  
  20. program Which; uses Dos;
  21.  
  22. function Search(var Target, Path, Result: String): Boolean;
  23.   function FileFound(F: String): Boolean;
  24.   begin
  25.     Result := FSearch(F, Path);
  26.     FileFound := Length(Result) > 0
  27.   end;
  28. begin
  29.   if Pos('.', Target) > 0 then
  30.     Search := FileFound(Target)
  31.   else begin
  32.     Search := True;
  33.     if not FileFound(Target + '.COM') then
  34.     if not FileFound(Target + '.EXE') then
  35.     if not FileFound(Target + '.BAT') then
  36.       Search := False
  37.   end
  38. end;
  39.  
  40. function NextPath(var Path: String): String;
  41. var
  42.   P: Byte;
  43. begin
  44.   P := Pos(';', Path);
  45.   if P = 0 then
  46.   begin
  47.     NextPath := Path;
  48.     Path := ''
  49.   end else
  50.   begin
  51.     NextPath := Copy(Path, 1, P - 1);
  52.     Delete(Path, 1, P)
  53.   end
  54. end;
  55.  
  56. procedure Instruct;
  57. begin
  58.   Writeln;
  59.   Writeln('WHICH.EXE');
  60.   Writeln('Copyright (C) 1993 by Tom Swan.');
  61.   Writeln;
  62.   Writeln('Enter WHICH NAME to find which executable code');
  63.   Writeln('file--NAME.COM, NAME.EXE, or NAME.BAT--would be');
  64.   Writeln('executed if you type NAME at a DOS prompt.');
  65.   Halt
  66. end;
  67.  
  68. var
  69.   I: Integer;
  70.   Found: Boolean;
  71.   Target, SysPath, Path, Result: String;
  72.  
  73. begin
  74.   if ParamCount < 1 then Instruct;
  75.   Target := ParamStr(1);
  76.   for I := 1 to Length(Target) do
  77.     Target[I] := Upcase(Target[I]);
  78.   SysPath := GetEnv('PATH');
  79.   Path := '';  { Current directory }
  80.   Found := Search(Target, Path, Result);
  81.   while (not Found and (Length(SysPath) > 0)) do
  82.   begin
  83.     Path := NextPath(SysPath);
  84.     Found := Search(Target, Path, Result)
  85.   end;
  86.   if Found
  87.     then Writeln(Result)
  88.     else Writeln(Target, ' not found')
  89. end.
  90.  
  91.  
  92. (*
  93. // --------------------------------------------------------------
  94. // Copyright (c) 1993 by Tom Swan. All rights reserved
  95. // Revision 1.00    Date: 07/03/1992   Time: 04:16 pm
  96. // Revision 2.00    Date: 03/04/1993   Time: 07:46 am
  97. // - Fixed bugs in earlier version (complete rewrite)
  98. *)
  99.